package bb.lanxing.activity.discover;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.adapter.discover.BbsReportAdapter;
import bb.lanxing.model.database.BbsType;
import bb.lanxing.network.BiCiCallback;
import bb.lanxing.network.MyHttpClient;
import okhttp3.Request;

public class BbsReportActivity extends BaseActivity {
    private BbsReportAdapter mBbsReportAdapter = new BbsReportAdapter();
    private ArrayList<BbsType> bbsReportTypesList = new ArrayList<>();
    private String typeName;
    private int typeId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bbs_report);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        RecyclerView bbsList = findViewById(R.id.bbsList);
        bbsList.setLayoutManager(new LinearLayoutManager(this));
        mBbsReportAdapter.setOnItemClickListener(position -> {
            mBbsReportAdapter.bbsReportType = position;
            typeName = bbsReportTypesList.get(position).typeName;
            typeId = bbsReportTypesList.get(position).typeId;
        });
        bbsList.setAdapter(mBbsReportAdapter);

        String content = getIntent().getStringExtra("content");
        int blogId = getIntent().getIntExtra("blogId", 0);
        int commentId = getIntent().getIntExtra("commentId", 0);

        TextView commitBtn = findViewById(R.id.commitBtn);
        commitBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, BbsReportCommitActivity.class);
            intent.putExtra("content", content);
            intent.putExtra("blogId", blogId);
            intent.putExtra("commentId", commentId);
            intent.putExtra("typeId", typeId);
            intent.putExtra("typeName", typeName);
            startActivity(intent);
        });

        loadData();
    }

    private void loadData() {
        BiCiCallback biCiCallback = new BiCiCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                super.onFailure(request, e);
            }

            @Override
            public void onResponseString(@NonNull String string) {
                System.out.println("string1: " + string);
                if (!TextUtils.isEmpty(string)) {
                    try {
                        JSONObject jsonObject = new JSONObject(string);
                        int code = jsonObject.optInt("code");
                        if (200 == code) {
                            try {
                                JSONArray jSONArray = new JSONObject(string).getJSONArray("rows");
                                for (int i = 0; i < jSONArray.length(); i++) {
                                    BbsType createByJson = BbsType.createByJson(jSONArray.getJSONObject(i));
                                    bbsReportTypesList.add(createByJson);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            runOnUiThread(() -> {
                                mBbsReportAdapter.updateData(bbsReportTypesList);
                            });
                        }
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    toast("获取类型列表失败");
                }
            }
        };
        MyHttpClient.requestReportTypeList(biCiCallback);
    }
}